//
//  HJStatusOriginalView.swift
//  Micro-blog Sina
//
//  Created by admin on 16/3/25.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SnapKit

/// 原创微博的view
class HJStatusOriginalView: UIView {
    
    var originalViewBottomConstraint: Constraint?
    
    var status: HJStatuses? {
        didSet {
            if let imageURL = status?.user?.profile_image_url {
                let url = NSURL(string: imageURL)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            nameLabel.text = status?.user?.screen_name
//            timeLabel.text = status?.created_at
            timeLabel.text = NSDate.sinaDateToDate(status?.created_at ?? "刚刚").sinaDateDescription()
            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            memberView.image = status?.user?.mbrankImage
            verifiedView.image = status?.user?.verifiedViewImage
            originalViewBottomConstraint?.uninstall()
            
            if let pictureURLs = status?.storePictureURLs {
                // 有原创微博配图
                pictureView.hidden = false
                pictureView.pictureURLs = pictureURLs
                self.snp_makeConstraints { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HJStatusCellMargin).constraint
                }
            }else {
                // 没有原创微博配图
                pictureView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HJStatusCellMargin).constraint
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        prepareUI()
    }
    
    private func prepareUI() {
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HJStatusCellMargin)
            make.leading.equalTo(self).offset(HJStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(HJStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(HJStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HJStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(HJStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(HJStatusCellMargin)
            make.leading.equalTo(self).offset(HJStatusCellMargin)
            make.trailing.equalTo(self).offset(-HJStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HJStatusCellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(HJStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HJStatusCellMargin).constraint
        }

    }
    
    // MARK: - Lazy load
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(9)
        return label
    }()
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(9)
        return label
    }()
    private lazy var memberView: UIImageView = UIImageView()
    private lazy var verifiedView: UIImageView = UIImageView()
    private lazy var contentLabel: UILabel = {
        let label =  UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        return label
    }()
    private lazy var pictureView: HJStatusPictureView = HJStatusPictureView()
}
